package phaseII;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import phaseIref.DFA;
import phaseIref.NFA;
import phaseIref.Parser;
import phaseIref.ScannerGenerator;

public class Executioner {
	private Map<String, NFA> charClasses;
	private HashMap<String, Integer> priorityMap;
	
	/** 
	 *  @return ArrayList<String> of matched tokens, null if no file found 
	 */
	public ArrayList<String> find(String regex, String textFile) {
		ArrayList<String> retval = new ArrayList<String>();
		DFA dfa = new DFA(Parser.parse("$WORD " + regex, charClasses), priorityMap);
		
		try {
			for (phaseIref.Token token:dfa.tokenize(new Scanner(new File(textFile)))) {
				retval.add(token.getString());
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.exit(-1);
		}
		
		return retval;
	}
	
	public boolean replace(String regex, String replacement, String fileIn, String fileOut) {
		boolean replaced = false;
		BufferedReader input;
		BufferedWriter output;
		
		try {
			input = new BufferedReader(new FileReader(fileIn));
		} catch (FileNotFoundException fnfe) {
			fnfe.printStackTrace();
			System.exit(-1);
			return false;
		}
		
		try {
			output = new BufferedWriter(new FileWriter(fileOut));
		} catch (IOException ioe) {
			ioe.printStackTrace();
			try {
				input.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			System.exit(-1);
			return false;
		}
		
		while(true) {
			//TODO: Actual logic
			break;
		}
		
		try {
			input.close();
			output.close();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
		
		return replaced;
	}
	
	public void recursiveReplace(String regex, String replacement, String fileIn, String fileOut) {
		String tempFile = "temp.txt";
		boolean replaced = replace(regex, replacement, fileIn, fileOut);
		
		do {
			replaced = replace(regex, replacement, fileIn, fileOut);
			fileIn = tempFile;
			tempFile = fileOut;
			fileOut = fileIn;
		} while(replaced);
		// TODO: delete tempFile
	}
	
	public String findMaxFreqString(ArrayList<String> tokens) {
		String retval = null;
		// TODO: Actual logic
		return retval;
	}
	
	public ArrayList<String> inters(ArrayList<String> a, ArrayList<String> b) {
		@SuppressWarnings("unchecked")
		ArrayList<String> retval = (ArrayList<String>)a.clone();
		retval.retainAll(b);
		return retval;
	}
	
	public ArrayList<String> union(ArrayList<String> a, ArrayList<String> b) {
		@SuppressWarnings("unchecked")
		ArrayList<String> retval = (ArrayList<String>)a.clone();
		retval.addAll(b);
		return retval;
	}
	
	public ArrayList<String> diff(ArrayList<String> a, ArrayList<String> b) {
		@SuppressWarnings("unchecked")
		ArrayList<String> retval = (ArrayList<String>)a.clone();
		retval.removeAll(b);
		return retval;
	}
	
	public Executioner() {
		charClasses = ScannerGenerator.readAndBuildCharClasses("$ASCII [ -~]\n");
		priorityMap = new HashMap<String, Integer>();
		priorityMap.put("$WORD", 0);
	}
}
